﻿$(document).ready(function () {
    $("#btnOk").hide();
});

function add(obj) {
    var id = $(obj).parent().parent().attr("id");
    var projectId = $("#ProjectId").val();
    var keyWord = $("#SearchMember").val();
    var method = "add";
    var groupMemberPageIndex = $("#groupMemberPageIndex").html();
    if ($("#groupMemberPageIndex").html() == "") {
        groupMemberPageIndex = 1;
    }
    var projectMemberPageIndex = $("#projectMemberPageIndex").html();
    if ($("#projectMemberPageIndex").html() == "") {
        projectMemberPageIndex = 1;
    }
    var requestObj = {
        Id: id,
        KeyWord: keyWord,
        Method: method,
        ProjectId: projectId,
        GroupMemberPageIndex: groupMemberPageIndex,
        ProjectMemberPageIndex: projectMemberPageIndex,
        ReturnType: "json",
        IsAssigned: false
    };
    send(requestObj, false);
    return false;
}

function remove(obj) {
    var id = $(obj).parent().parent().attr("id");
    var projectId = $("#ProjectId").attr("value");
    var keyWord = $("#SearchMember").val();
    var method = "remove";
    var groupMemberPageIndex = $("#groupMemberPageIndex").html();
    if ($("#groupMemberPageIndex").html() == "") {
        groupMemberPageIndex = 1;
    }
    var projectMemberPageIndex = $("#projectMemberPageIndex").html();
    if ($("#projectMemberPageIndex").html() == "") {
        projectMemberPageIndex = 1;
    }
    var requestObj = {
        Id: id,
        KeyWord: keyWord,
        Method: method,
        ProjectId: projectId,
        GroupMemberPageIndex: groupMemberPageIndex,
        ProjectMemberPageIndex: projectMemberPageIndex,
        ReturnType: "json",
        IsAssigned: false
    };
    send(requestObj, false);
    return false;
}

function changeAssign(obj) {
    var id = $(obj).parent().parent().attr("id");
    var projectId = $("#ProjectId").attr("value");
    var isAssigned = obj.checked;
    var keyWord = $("#SearchMember").val();
    var method = "toggleAssigned";
    var groupMemberPageIndex = $("#groupMemberPageIndex").html();
    if ($("#groupMemberPageIndex").html() == "") {
        groupMemberPageIndex = 1;
    }
    var projectMemberPageIndex = $("#projectMemberPageIndex").html();
    if ($("#projectMemberPageIndex").html() == "") {
        projectMemberPageIndex = 1;
    }
    var requestObj = {
        Id: id,
        KeyWord: keyWord,
        Method: method,
        ProjectId: projectId,
        GroupMemberPageIndex: groupMemberPageIndex,
        ProjectMemberPageIndex: projectMemberPageIndex,
        ReturnType: "json",
        IsAssigned: isAssigned
    };
    send(requestObj, true);
}

function searchMember(input) {
    var keyWord = $(input).val();
    var pId = $("#ProjectId").attr("value");
    if (keyWord == "") {
        keyWord = "Search Member";
    }
    $.ajax({
        url: "/Project/SearchMember/" + pId + "/" + keyWord,
        type: "POST",
        traditional: true,
        success: function (data, status, xhr) {
            var onSuccess = new OnSuccess(data);
            onSuccess.removeOldData();
            onSuccess.addDataToTables();
            onSuccess.createPageNavigator();
        },
        error: function (xhr, status, err) {
            alert("An error occus on sending information. Please reload this page")
        }
    });
}

function send(obj, isChangeAssign) {
    $.ajax({
        url: "/Project/AddRemoveProjectMember/" + obj.Id + "/" + obj.ProjectId + "/" + obj.GroupMemberPageIndex + "/" + obj.ProjectMemberPageIndex + "/" + obj.Method + "/" + obj.KeyWord + "/" + obj.ReturnType + "/" + obj.IsAssigned,
        type: "POST",
        data: obj,
        datatype: "json",
        traditional: true,
        success: function (data, status, xhr) {
            if (typeof (data) == "string") {
                if (isChangeAssign) {
                    document.getElementById(obj.Id).childNodes[4].firstChild.checked = true;
                }
                alert(data);
            } else {
                var onSuccess = new OnSuccess(data);
                onSuccess.removeOldData();
                onSuccess.addDataToTables();
                onSuccess.createPageNavigator();
            }
        },
        error: function (xhr, status, err) {
            alert("An error occus on sending information. Please reload this page")
        }
    });
}

$("#loading").ajaxStart(function () {
    $(this).center();
    $(this).show();
});

$("#loading").ajaxStop(function () {
    $(this).hide();
});

function OnSuccess(jsonObj) {
    this.Json = jsonObj;
    //remove data from table
    this.removeOldData = removeOldData;
    this.addDataToTables = addDataToTables;
    this.createPageNavigator = createPageNavigator;
    this.removePageNavigator = removePageNavigator;
}

function removePageNavigator() {
    $("#ProjectMemberPageNavigator").html("");
    $("#GroupMemberPageNavigator").html("");
}

function removeOldData() {
    $("tr[id]").remove();
}

function addDataToTables() {
    //add data to projectMemberTable
    if (this.Json.ProjectMembers.length > 0) {
        for (i = 0; i < this.Json.ProjectMembers.length; i++) {
            var row = "<tr id='" + this.Json.ProjectMembers[i].AccountId + "'>";
            //                row += "<td><input  type='checkbox' value='" + data[i][0] + "'/></td>";
            row += "<td>" + this.Json.ProjectMembers[i].UserName + "</td>";
            row += "<td>" + this.Json.ProjectMembers[i].FirstName + "</td>";
            row += "<td>" + this.Json.ProjectMembers[i].LastName + "</td>";
            row += "<td>" + this.Json.ProjectMembers[i].Email + "</td>";
            if (this.Json.ProjectMembers[i].IsAssigned) {
                row += "<td><input type='checkbox' class='isAssigned' checked='checked' onclick='changeAssign(this)' /></td>";
            } else {
                row += "<td><input type='checkbox' class='isAssigned' onclick='changeAssign(this)' /></td>";
            }
            row += "<td><a class='remove' href='#' onclick='return remove(this)'>Remove</a></td>";
            row += "</tr>";
            $("#ProjectMembers").append(row);
        }
    }
    //add data to groupmember table
    if (this.Json.GroupMembers.length > 0) {
        for (i = 0; i < this.Json.GroupMembers.length; i++) {
            var row = "<tr id='" + this.Json.GroupMembers[i].AccountId + "'>";
            //                row += "<td><input  type='checkbox' value='" + data[i][0] + "'/></td>";
            row += "<td>" + this.Json.GroupMembers[i].UserName + "</td>";
            row += "<td>" + this.Json.GroupMembers[i].FirstName + "</td>";
            row += "<td>" + this.Json.GroupMembers[i].LastName + "</td>";
            row += "<td>" + this.Json.GroupMembers[i].Email + "</td>";
            row += "<td><a class='add' href='#' onclick='return add(this)'>Add</a></td>";
            row += "</tr>";
            $("#GroupMembers").append(row);
        }
    }
}

function createPageNavigator() {
    //create projectmember page navigator
    var projectMemberPageNavText = "";
    $("#proMemPrevious").html("");
    $("#proMemNext").html("");
    $("#groupPrevious").html("");
    $("#groupNext").html("");
    if (this.Json.projectMemberPageIndex > 1) {
        if (this.Json.keyWord == "Search Member") {
            projectMemberPageNavText = "<a class='page-far-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/1/" + this.Json.groupMembersPageIndex + "'></a>";
            projectMemberPageNavText += "<a class='page-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + (this.Json.projectMemberPageIndex - 1) + "/" + this.Json.groupMembersPageIndex + "'></a>";
        } else {
            projectMemberPageNavText = "<a class='page-far-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/1/" + this.Json.groupMembersPageIndex + "/" + this.Json.keyWord + "'></a>";
            projectMemberPageNavText += "<a class='page-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + (this.Json.projectMemberPageIndex - 1) + "/" + this.Json.groupMembersPageIndex + "/" + this.Json.keyWord + "'></a>";
        }
        $("#proMemPrevious").html(projectMemberPageNavText);
    }
    $("#projectMemberPageIndex").html(this.Json.projectMemberPageIndex);
    $("#totalProjectMemberPage").html(this.Json.totalProjectMemberPage);
    if (this.Json.projectMemberPageIndex < this.Json.totalProjectMemberPage) {
        if (this.Json.keyWord == "Search Member") {
            projectMemberPageNavText = "<a class='page-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + (this.Json.projectMemberPageIndex + 1) + "/" + this.Json.groupMembersPageIndex + "'></a>";
            projectMemberPageNavText += "<a class='page-far-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.totalProjectMemberPage + "/" + this.Json.groupMembersPageIndex + "'></a>";
        } else {
            projectMemberPageNavText = "<a class='page-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + (this.Json.projectMemberPageIndex + 1) + "/" + this.Json.groupMembersPageIndex + "/" + this.Json.keyWord + "'></a>";
            projectMemberPageNavText += "<a class='page-far-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.totalProjectMemberPage + "/" + this.Json.groupMembersPageIndex + "/" + this.Json.keyWord + "'></a>";
        }
        $("#proMemNext").html(projectMemberPageNavText);
    }

    //create groupmember page navigator
    var groupMemberPageNavText = "";
    if (this.Json.groupMembersPageIndex > 1) {
        if (this.Json.keyWord == "Search Member") {
            groupMemberPageNavText = "<a class='page-far-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/1'></a>";
            groupMemberPageNavText += "<a class='page-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + (this.Json.groupMembersPageIndex - 1) + "'></a>";
        } else {
            groupMemberPageNavText = "<a class='page-far-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/1/" + this.Json.keyWord + "'></a>";
            groupMemberPageNavText += "<a class='page-left' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + (this.Json.groupMembersPageIndex - 1) + "/" + this.Json.keyWord + "'></a>";
        }
        $("#groupPrevious").html(groupMemberPageNavText);
    }
    $("#groupMemberPageIndex").html(this.Json.groupMembersPageIndex);
    $("#totalGroupMemberPage").html(this.Json.totalGroupAccountPage);
    if (this.Json.groupMembersPageIndex < this.Json.totalGroupAccountPage) {
        if (this.Json.keyWord == "Search Member") {
            groupMemberPageNavText = "<a class='page-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + (this.Json.groupMembersPageIndex + 1) + "'></a>";
            groupMemberPageNavText += "<a class='page-far-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + this.Json.totalGroupAccountPage + "'></a>";
        } else {
            groupMemberPageNavText = "<a class='page-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + (this.Json.groupMembersPageIndex + 1) + "/" + this.Json.keyWord + "'></a>";
            groupMemberPageNavText += "<a class='page-far-right' href='/Project/ChangeProjectMember/" + this.Json.projectId + "/" + this.Json.projectMemberPageIndex + "/" + this.Json.totalGroupAccountPage + "/" + this.Json.keyWord + "'></a>";
        }
        $("#groupNext").html(groupMemberPageNavText);
    }
}